from django.template import Library
from django.urls import reverse, resolve  # py中反向解析url地址

from web import models

register = Library()


@register.simple_tag
def user_space(size):
    """ 字节传兆 """
    if size >= 1024 * 1024 * 1024:  # 如果大于一个G 就转化成GB
        size_gb = size / (1024 * 1024 * 1024)
        return "%.2f GB" % size_gb
    elif size >= 1024 * 1024:  # 如果大于一兆 就转化成M
        size_mb = size / (1024 * 1024)
        return "%.2f MB" % size_mb
    elif size >= 1024:  # 如果大于一KB 就转化成kb
        size_kb = size / 1024
        return "%.2f KB" % size_kb
    else:  # 都不是的话就返回原值
        return "%d B" % size
